/*
 * @Author: your name
 * @Date: 2021-01-15 13:23:19
 * @LastEditTime: 2021-01-21 17:18:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \easycmsboot\src\main\java\com\easycms\web\controller\system\SysMenuController.java
 */
package com.easycms.web.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import com.easycms.framework.web.ActionResult;
import com.easycms.framework.web.BaseController;
import com.easycms.framework.domain.ResultAdaptDto;
import com.easycms.framework.domain.KeyValueInt64Dto;
import com.easycms.web.system.domain.SysMenu;
import com.easycms.web.system.dto.*;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.easycms.web.system.service.*;

/**
 @className SysMenuController
 @author yushuo
 **/
@Controller
@RequestMapping("/admin/sysmenu")
public class SysMenuController extends BaseController {

    //private String PAGE_PREFIX="admin/sysmenu/";

    @Autowired
    private SysMenuService menuService;

    /**
     * 列表页面
     * @param model
     * @return
     */
    @RequiresPermissions("system:menu:view")
    @GetMapping("/index")
    public ActionResult index(ModelMap model){

        model.put("nowtime", DateUtil.now());
        return view();
        //return PAGE_PREFIX+"index";
    }

    /**
     * 返回菜单列表
     * @return
     */
    @RequiresPermissions("system:menu:view")
    @GetMapping("/getMenuList")
    @ResponseBody
    public List<SysMenu> getMenuList(){
        List<SysMenu> menus=menuService.getMenus();
        return menus;
    }


    @GetMapping("/form")
    @RequiresPermissions(value = {"system:menu:add","system:menu:edit"},logical = Logical.OR)
    public ActionResult form(String id,ModelMap model){
        if(StrUtil.hasBlank(id)){
            id="0";
        }
       
        model.addAttribute("id", id);
        List<KeyValueInt64Dto> menuSel=menuService.getMenuSelect();
        model.put("menuSel", menuSel);
        return view();
    }

    @RequiresPermissions(value = {"system:menu:add","system:menu:edit"},logical = Logical.OR)
    @GetMapping("/getmodel")
    public ActionResult getModel(String id){
        ResultAdaptDto result=new ResultAdaptDto();
        MenuDto menu=menuService.getMenuModel(id);
        result.getData().put("model",menu);
        return json(result);
    }

    @RequiresPermissions(value = {"system:menu:add","system:menu:edit"},logical = Logical.OR)
    @PostMapping("/SaveData")
    public ActionResult saveData(SysMenu menuDto,@RequestParam String funcs){
        ResultAdaptDto result=new ResultAdaptDto();

        result.getData().put("menu",menuDto);
        result.getData().put("funcs",funcs);
       // String funcs=map.get("funcs").toString();
        menuService.saveMenu(menuDto,funcs);
        return ok("数据保存成功");
    }

    /**
    *
     * @author yushuo
     * @description 删除菜单
     * @date 10:34 2021/1/29
     * @param
     * @return com.easycms.framework.domain.ResultAdaptDto
    **/
    @RequiresPermissions("system:menu:delete")
    @GetMapping("/delById")
    public ActionResult delById(long id){

         if(id<1){
             return error("菜单数据不存在");
         }
         menuService.delById(id);
         return ok("删除成功");
    }
}
